



//====================================================================
//		Routine: 	putUART
//		Description:Used to transmit a byte via standard 8N1 UART protocol
//		may be called from any thread
//		r6			= in: return address
//		r1			= in: data byte to send (scrambled)
//		r0,r2-r5		= scratchpad registers
//		TX pin		= GPA0
//		requires GPA0 to be setup as output and idling high
//		BaudRate set by setting constant UARTbitTime(measured in system clock cycles)
//		UARTbitTime = (1/baudrate) / (1/systemClock)
//		Using 12MHz clock, 115200 baudrate can be achieved with this routine but produces gitter
//		For move accuracy at high baudrates a modified routine that does timing by counting
//		 Instruction times for each bit would provide more accuracy.
//
//		BaudRate table	(for system clock = 12MHz)
//
//		baudRate	bitTime(us)		UARTbitTime
//		9600		104.1667		1250
//		57600		17.3611			208.333
//		115200		8.680556		104.17
//====================================================================

		UARTbitTime = 1250			//set baud rate to 9600


putUART:
		mov		r5,UARTbitTime
		bis		r1,r1,8			//add stop bit
		rol		r1,r1,1

		mov		r0,0				//setup start bit
		inp		r2,SCUtime			//get time of start bit
		outp		r0,GPAout			//send start bit
		mov		r4,10				//initialize loop counter

putUARTloop:
		sub		r4,r4,1			//decrement loop counter
		bc		ZS,done_putUART		//test for done
		add		r2,r2,r5			//increment bit timer
		rol		r1,r1,-1			//shift right
		bc		VC,send_zero		//bit test
send_one:
		mov		r0,1				//txpin is GPA0
send_oneWait:
		inp		r3,SCUtime			//get current time
		sub		r3,r3,r2
		bc		LT0,send_oneWait	//wait for 1 bit time
		outp		r0,GPAout
		bra		putUARTloop

send_zero:
		mov		r0,0				//txpin is GPA0
send_zeroWait:
		inp		r3,SCUtime			//get current time
		sub		r3,r3,r2
		bc		LT0,send_zeroWait	//wait for 1 bit time
		outp	r0,GPAout
		bra		putUARTloop

done_putUART:
		jsr		r6,r6				//return



//====================================================================
//		Routine: 	getUART
//		Description:Used to receive a byte via standard 8N1 UART protocol
//		may be called from any thread
//		r6			= in: return address
//		r1			= out: received data byte,bit 15 of r1 is set if no valid stop bit received
//		r0,r2-r5	= scratchpad registers
//		RX pin		= GPA1
//		see "putUART" routine for BaudRate settings
//====================================================================

getUART:
		mov		r1,0				//r1 = receive data byte
		mov		r4,9				//loop counter
		mov		r5,UARTbitTime
		rol		r3,r5,-1			//divide by 2
		bic		r3,r3,15
		add		r3,r3,r5			//calculate UARTbitTime + halfbitTime

startDetect:
		inp		r2,SCUtime			//time of startbit
		inp		r0,GPAin
		bic		r0,r0,1				//test GPA1
		bc		VS,startDetect		//wait for start bit

		add		r2,r2,r3 			//sample in the middle of the bit
bitwait:
		inp		r3,SCUtime			//get current time
		sub		r3,r3,r2
		bc		LT0,bitwait			//wait until half bit time

		inp		r0,GPAin
		bic		r0,r0,1				//test GPA1
		bc		VS,got_one
got_zero:							//data register already initialized to 0
		bra		gotbit
got_one:
		bis		r1,r1,0				//received a one
gotbit:
		rol		r1,r1,-1			//rotate data register(read LSB first)
		add		r2,r2,r5			//increment bit time
		sub		r4,r4,1				//decrement loop counter
		bc		ZC,bitwait

		rol		r1,r1,-7			//rotate data into LSB
		bic		r1,r1,8
		bc		VS,ValidStopBit		//test for stop bit

		bis		r1,r1,15			//set flag bit
ValidStopBit:
		jsr		r6,r6				//return